/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lhh.apst.advancedpagerslidingtabstrip.slice;

import com.lhh.apst.advancedpagerslidingtabstrip.*;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

/**
 * MainAbilitySlice
 *
 * @since 2021-04-30
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button mNormalTab;
    private Button mIconTab;
    private Button mCustomTab;
    private Button mViewTab;
    private Button mWeiboTab;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        findViews();
        setButton();
    }

    private void findViews() {
        mIconTab = (Button) findComponentById(ResourceTable.Id_icontab);
        mNormalTab = (Button) findComponentById(ResourceTable.Id_noramltab);
        mCustomTab = (Button) findComponentById(ResourceTable.Id_customtab);
        mViewTab = (Button) findComponentById(ResourceTable.Id_viewtab);
        mWeiboTab = (Button) findComponentById(ResourceTable.Id_weibotab);
    }

    private void setButton() {
        mIconTab.setClickedListener(this);
        mNormalTab.setClickedListener(this);
        mCustomTab.setClickedListener(this);
        mViewTab.setClickedListener(this);
        mWeiboTab.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_icontab:
                launchAbility(new IconTabAbility());
                break;
            case ResourceTable.Id_noramltab:
                launchAbility(new NormalTabAbility());
                break;
            case ResourceTable.Id_customtab:
                launchAbility(new CustomTabAbility());
                break;
            case ResourceTable.Id_viewtab:
                launchAbility(new ViewTabAbility());
                break;
            case ResourceTable.Id_weibotab:
                launchAbility(new WeiboTabAbility());
                break;
            default:
                break;
        }
    }

    private void launchAbility(Ability ability) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withDeviceId("")
                .withAbilityName(ability.getClass().getName())
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }
}
